<?php

App::uses('AclUtil', 'Lib');

class ProfileController extends AppController {
    var $helpers = array ('Html','Form','Session');
    var $name = 'Profile'; // riferimento alla cartella delle view
    //var $modelClass = 'Multa';
    
    public function beforeFilter(){
    	
		parent::beforeFilter();
		// tutte le azioni di questo controller possono essere eseguite
		// $this->Auth->allow("index");
		
		$aros = $this->Acl->Aro->generateTreeList(array("Aro.model"=>"Group"),"{n}.Aro.alias","{n}.Aro.alias","&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		$this->set("aros",$aros);
		$acos = $this->Acl->Aco->generateTreeList(null,"{n}.Aco.id","{n}.Aco.alias","&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		$this->set("acos",$acos);
		
    }

	public function beforeRender(){
		if(isset($this->viewVars['aros_acos'])) {
				$aros_acos = $this->viewVars['aros_acos'];
				// print_r($aros_acos);die;
				foreach ($aros_acos as $key => $aro_aco) {
					// print_r($aro_aco);die;
					$alias = AclUtil::getAcoParentAlias($this, $aro_aco['acos']['parent_id']);
					if (!empty($alias)) {
						$this->viewVars['aros_acos'][$key]['acos']['aco']=$alias."/".$aro_aco['acos']['aco'];
					}
				}
		}
	}

	
	public function index($tab = null) {
		
		$this->set("tab",$tab);
		// Controller::loadModel('Acl');
		// $aros = $this->Acl->Aro->find("all",array("recursive"=>0));
		// $aros = $this->Acl->Aro->find("threaded",array("recursive"=>0));
		// $aros = $this->Acl->Aro->generateTreeList(null,"Aro.id","Aro.Alias"," ",2);
		// $aros = $this->Acl->Aro->generateTreeList(null,"{n}.Aro.id","{n}.Aro.alias","&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		// $this->set("aros",$aros);
		// $acos = $this->Acl->Aco->generateTreeList(null,"{n}.Aco.id","{n}.Aco.alias","&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		// $this->set("acos",$acos);
		
		$this->loadModel('Dual');
		$sql = "select aros_acos.id, aros.alias aro,acos.alias aco,aros_acos._create, aros_acos._read, aros_acos._update, aros_acos._delete, acos.parent_id  from aros_acos inner join acos on aros_acos.aco_id = acos.id inner join aros on aros_acos.aro_id = aros.id";
		$aros_acos = $this->Dual->query($sql);
		$this->set("aros_acos",$aros_acos);
		
	}
	
	public function addAco() {
		$this->saveAco();
	}
	
	public function saveAco() {
		
		$this->Acl->Aco->create($this->request->data["Aco"]);
		$this->Acl->Aco->save();
		$this->Session->setFlash(__('Oggetto Aco aggiornato.'));
		$this->redirect("/Profile/index/hTabAco");
		
	}
	
	public function deleteAco($id) {
		$this->Acl->Aco->delete($id);
		$this->Session->setFlash(__('Oggetto Aco eliminato.'));
		$this->redirect("/Profile/index/hTabAco");
	}
	
	public function addAllowDeny() {
		$this->saveAllowDeny();
		$this->Session->setFlash(__('Permesso Inserito.'));
		$this->redirect("/Profile/index/hTabPermessi");
				
	}
	
	public function deleteAllowDeny($id) {
		$this->loadModel('AroAco');
		$this->AroAco->delete($id);
		$this->Session->setFlash(__('Permesso eliminato.'));
		$this->redirect("/Profile/index/hTabPermessi");
	}

	private function saveAllowDeny() {
		$permesso = $this->request->data["permesso"];
		$azioni = null;
		if(isset($this->request->data["azioni"])) {
			$azioni = $this->request->data["azioni"];
		}
		$aroAlias = $this->request->data["aro"];
		$acoId = $this->request->data["aco"];
		$aco = $this->Acl->Aco->read(null,$acoId);
		$acoAlias = AclUtil::getAcoParentAlias($this,$aco["Aco"]["parent_id"]);
		// print_r($acoAlias."/".$aco["Aco"]["alias"]);
		if (empty($azioni))
			$this->Acl->{$permesso}($aroAlias, $acoAlias."/".$aco["Aco"]["alias"]);
		else
			$this->Acl->{$permesso}($aroAlias, $acoAlias."/".$aco["Aco"]["alias"], $azioni);
	}
	
	

}
?>